/*
 * Created on 2006-2-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.net;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.util.exception.GSException;


/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Download {
    
    
    public static Download download = new Download();
    
    public Download(){
        init();
    }
    
    public static Download getInstance(){
        return download; 
    }
    
    public void setDownloadPath(String path){
        
    }
    
    /**
     * 初始化文件下载的本地缓存路径
     *
     */
    public static void init(){
        
        //try{
	        File pathFile = new File(GSSystemConfigurator.getParam("gs.auth.savepath"));
	        if(!pathFile.exists()){
	            pathFile.mkdir();
	        }
        //}catch(IOException ioe){
        //    GSLogWriter.fatal(GSException.BME17778,GSException.BME17778_MSG);
        //}

    }

    /**
     * 传输订单明细对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionAllItmesFile(String date) throws GSException {
        String fileName = GSAuditConstants.getFileNameSimple(date,GSAuditConstants.ALLITEMS);
        trans(fileName);
    }
    
    /**
     * 传输送货对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionItemsDoingFile(String date) throws GSException {
        String fileName = GSAuditConstants.getFileNameSimple(date,GSAuditConstants.ITEMSDOING);
        trans(fileName);
    }
     
    /**
     * 传输拒绝送货对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionItemsRefusedFile(String date) throws GSException { 
        String fileName = GSAuditConstants.getFileNameSimple(date,GSAuditConstants.ITEMSREFUSED);
        trans(fileName);
    }
    
    /**
     * 传输交货对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionItemsFinishedFile(String date) throws GSException {
        String fileName = GSAuditConstants.getFileNameSimple(date,GSAuditConstants.ITEMSFINISHED);
        trans(fileName);
    }
    
    /**
     * 传输明细取消对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionItemsCancelFile(String date) throws GSException { 
        String fileName = GSAuditConstants.getFileNameSimple(date,GSAuditConstants.ITEMSCANCEL);
        trans(fileName);
    }

    
    /**
     * 具体的文件下载执行器
     * @param fileName 要下载的文件名，带url
     * @throws GSException
     */
    private void trans(String fileName) throws GSException {
        FileInputStream fis = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        try{
            URL url = new URL(GSSystemConfigurator.getParam("gs.auth.schema") + "://" + GSSystemConfigurator.getParam("gs.auth.host") + ":" + GSSystemConfigurator.getParam("gs.auth.port") + "/" + GSSystemConfigurator.getParam("gs.auth.fullpath") + "/" + fileName);
                
            java.io.File f2 = new File(GSSystemConfigurator.getParam("gs.auth.savepath") + System.getProperty("file.separator") + fileName);
            if(!f2.exists()) {
                dis = new DataInputStream(url.openStream());

                f2.createNewFile();
                fos = new FileOutputStream(f2);
                
                byte[] buffer = new byte[512];
                int length = 0;
                while((length = dis.read(buffer,0,512))!=-1){
                    fos.write(buffer,0,length);
                }
                
                fos.flush();
                fos.close();
                dis.close();
            }
        }
        catch(IOException ioe){
            ioe.printStackTrace();
            throw new GSException (GSException.E17780,GSException.E17780_MSG);
        }
    }

}
